/*
 * @Author: 晟松
 * @Date: 2021-12-13 13:05:58
 * @LastEditors: 晟松
 * @LastEditTime: 2022-02-04 22:35:26
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\util\XlsxUtils.js
 * @Description: 解析xlsx格式的表格文件，主要是用于处理居委会和辅导员上传的学生和居民信息
 */
let xlsx = require("node-xlsx");
import enums from "@main/config/enums";
const fs = require("fs");
const path = require("path");

/**
 * @description: 将 '男' '女'转换为enums里面对应的number，使其进行存储
 * @param {*}
 * @return {*}
 */
export const genderTrans = (gender) => {
    if (gender == "男") {
        return "male";
    } else if (gender == "女") {
        return "female";
    } else {
        return "unknown";
    }
};

export const gradeTrans = (grade) => {
    let res = "";
    switch (grade) {
        case "大一":
            res = enums.grade[1];
            break;
        case "大二":
            res = enums.grade[2];
            break;
        case "大三":
            res = enums.grade[3];
            break;
        case "大四":
            res = enums.grade[4];
            break;
        case "研究生":
            res = enums.grade[8];
            break;
        default:
            break;
    }
    return res;
};

/**
 * @description:
 * @param {*} fileName xlsx文件的名字，因为文件都是存在upload目录下的
 * @param {*} type xlsx文件是居民的还是学生的，默认居民
 * @return {*}
 */
export const xlsxAnalysis = (fileName, type = "resident", filePath) => {
    console.log("fileName1: ", fileName, typeof fileName);
    fileName = String(fileName)
    console.log("filePath1: ", filePath, typeof filePath);
    if (!filePath) filePath = path.resolve(__dirname, "../../upload/" + fileName);

    console.log("filePath2: ", filePath, typeof filePath);

    // 解析得到文档中的所有 sheet
    let sheets = xlsx.parse(filePath);

    let minePath = path.resolve("D:\\Code\\node\\koa2\\volunteer-service\\src\\upload\\" + "testStudent-1.xlsx");
    console.log("minePath1: ", minePath, typeof minePath);


    fileName = String("1643985242915-testStudent.xlsx")
    console.log("fileName2: ", fileName, typeof fileName);
    filePath = path.resolve(__dirname, "../../upload/"+ fileName);
    console.log('filePath3: ', filePath);


    console.log("sheets: ", sheets, fs.readFileSync(minePath), fs.readFileSync(filePath));
    // console.log("sheets: ", sheets[0].data);

    /* 三个元素是因为excel有三个工作表
sheets:  [
    {
      name: 'Sheet1',
      data: [
        [Array], [Array],
        [Array], [Array],
        [Array], [Array],
        [Array], [Array]
      ]
    },
    { name: 'Sheet2', data: [] },
    { name: 'Sheet3', data: [] }
  ] */

    var arr = [];
    //sheets是一个数组，数组中的每一项对应test.xlsx这个文件里的多个表格，如sheets[0]对应test.xlsx里的“测试参数”这个表格，sheets[1]对应Sheet2这个表格
    sheets.forEach(function (sheet) {
        var newSheetsArr = [];
        //sheet是一个json对象，格式为{name:"测试参数",data:[]},我们想要的数据就存储在data里
        // 从2开始，因为Excel第一行是这个:账号	姓名	性别	年龄	学校...，第二行是填写示例
        for (var i = 2; i < sheet["data"].length; i++) {
            //excel文件里的表格一般有标题所以不一定从0开始
            var row = sheet["data"][i];
            if (row && row.length > 0) {
                // 注意需要对里面的性别，年级等进行转换,密码为默认账号后六位
                // 需转为string类型，似乎是因为电话号位数不会识别成文本类型
                let phone = row[0] + "";
                let password = phone.slice(-6);
                if (type == "resident") {
                    newSheetsArr.push({
                        identity: type,
                        password,
                        phone,
                        userName: row[1],
                        gender: genderTrans(row[2]),
                        age: row[3],
                        communityName: row[4],
                    });
                } else if (type == "student") {
                    newSheetsArr.push({
                        identity: type,
                        password,
                        phone,
                        userName: row[1],
                        gender: genderTrans(row[2]),
                        age: row[3],
                        schoolName: row[4],
                        college: row[5],
                        major: row[6],
                        studentId: row[7],
                        grade: gradeTrans(row[8]),
                    });
                }
            }
        }
        arr.push(newSheetsArr);
    });
    // 只返回第一个工作表的数据，为一个数组
    return arr[0];
};
